package com.ibeeking.found.file.rest.controller.v1;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.file.rest.service.IFileService;
import com.ibeeking.found.file.rest.service.IFiletransferService;
import com.ibeeking.found.file.rest.service.IRecoveryFileService;
import com.ibeeking.found.file.rest.service.IUserFileService;
import com.ibeeking.found.file.rest.service.component.AsyncTaskComp;
import com.ibeeking.found.file.service.common.dos.RecoveryFileDO;
import com.ibeeking.found.file.service.common.param.BatchDeleteRecoveryFileParam;
import com.ibeeking.found.file.service.common.param.DeleteRecoveryFileParam;
import com.ibeeking.found.file.service.common.param.RestoreFileParam;
import com.ibeeking.found.file.service.common.vo.RecoveryFileListVO;
import com.ibeeking.nematos.constants.enums.OperateLogTypeEnum;
import com.ibeeking.nematos.log.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
/**
 * @ClassName OfficeController
 * @Description 文件删除后会进入回收站，该接口主要是对回收站文件进行管理
 * @Author ibeeking
 * @Date 2021-06-02 18:15
 **/
@RestController
@Slf4j
@RequestMapping("/recoveryfile/v1")
public class RecoveryFileController {
    @Resource
    private IRecoveryFileService recoveryFileServiceImpl;

    @Resource
    private IUserFileService userFileServiceImpl;

    @Resource
    private IFileService fileServiceImpl;

    @Resource
    private IFiletransferService filetransferServiceImpl;

    @Resource
    private AsyncTaskComp asyncTaskComp;


    public static final String CURRENT_MODULE = "回收站文件接口";

    @Log(logType = OperateLogTypeEnum.DEL, describe = "删除回收文件")
    @PostMapping(value = "/deleterecoveryfile")
    public Boolean deleteRecoveryFile(@RequestBody DeleteRecoveryFileParam deleteRecoveryFileDTO) {
        RecoveryFileDO recoveryFile = recoveryFileServiceImpl.getOne(new QueryWrapper<RecoveryFileDO>().lambda().eq(RecoveryFileDO::getUserFileId, deleteRecoveryFileDTO.getUserFileId()));

        asyncTaskComp.deleteUserFile(recoveryFile.getUserFileId());

        recoveryFileServiceImpl.removeById(recoveryFile.getRecoveryFileId());
        return true;
    }

    @Log(logType = OperateLogTypeEnum.DEL, describe = "批量删除回收文件")
    @PostMapping(value = "/batchdelete")
    public Boolean batchDeleteRecoveryFile(@RequestBody BatchDeleteRecoveryFileParam batchDeleteRecoveryFileDTO) {
        String userFileIds = batchDeleteRecoveryFileDTO.getUserFileIds();
        String[] userFileIdList = userFileIds.split(",");
        for (String userFileId : userFileIdList) {
            RecoveryFileDO recoveryFile = recoveryFileServiceImpl.getOne(new QueryWrapper<RecoveryFileDO>().lambda().eq(RecoveryFileDO::getUserFileId, userFileId));

            if (recoveryFile != null) {
                asyncTaskComp.deleteUserFile(recoveryFile.getUserFileId());

                recoveryFileServiceImpl.removeById(recoveryFile.getRecoveryFileId());
            }

        }
        return true;
    }


    @Log(logType = OperateLogTypeEnum.QUERY, describe = "回收文件列表")
    @GetMapping(value = "/list")
    public List<RecoveryFileListVO> getRecoveryFileList() {
        Long userId = UserUtils.getUserId();
        List<RecoveryFileListVO> recoveryFileList = recoveryFileServiceImpl.selectRecoveryFileList(userId);
        return recoveryFileList;
    }

    @Log(logType = OperateLogTypeEnum.MODIFY, describe = "还原文件")
    @PostMapping(value = "/restorefile")
    public Boolean restoreFile(@RequestBody RestoreFileParam restoreFileDto) {
        Long userId = UserUtils.getUserId();
        recoveryFileServiceImpl.restorefile(restoreFileDto.getDeleteBatchNum(), restoreFileDto.getFilePath(), userId);
        return true;
    }

}









